const Model = require('../../models/index'),
    Activity = Model.Activity,
    express = require('express'),
    router = express.Router()
    // mg = require('mongoose')

router.post('/create', (req, res) => {
    Activity.find((err, data) => {
        if (err) {
            res.send(err)
            return false
        }
        let newEvent = new Activity({
            timeStart: req.body.timeStart,
            timeEnd: req.body.timeEnd,
            locationX: req.body.locationX,
            locationY: req.body.locationY,
            location: req.body.location,
            eid: req.body.eid,
            status: 'private',
            account: req.session.account
        })
        newEvent.save((err, data) => {
            if (err) {
                res.send(err)
            } else {
                res.send('success')
            }
        })
    })
})

router.put('/create', (req, res) => {
    Activity.findOneAndUpdate({eid: req.body.eid}, {$set: {status: req.body.status}}, (err, data) => {
        if (err) {
            res.send(err)
            return false
        }
        res.send('success')
    })
})

router.get('/list', (req, res) => {
    Activity.find({account: req.session.account}, 'timeStart timeEnd location status eid -_id ')
        .populate({path: 'event', select: 'label -_id'})
        .exec((err, data) => {
            if (err) {
                res.send(err)
                return false
            }
            res.send(data)
        })
})

module.exports = router
